package com.juvepoland.jpdroid.activities;

import com.juvepoland.jpdroid.R;
import com.juvepoland.jpdroid.application.JPdroidApplication;
import com.juvepoland.jpdroid.common.ImageLoader;
import com.juvepoland.jpdroid.managers.impl.UserManager;
import com.juvepoland.jpdroid.models.User;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Main menu activity, designed as dashboard, which is loaded 
 * as first after running application
 * ====================================================================
 * Aktywność głównego menu, uruchamiana jako pierwsza po starcie aplikacji.
 * Wykonana wg wzorca UI: Dashboard
 * @author Andrzej
 *
 */
public class MainDashboardActivity extends BasicActivity {

	protected Button newsButton;
	protected Button settingsButton;	
	protected Button infoButton;
	protected Button loginButton;
	
	protected User user;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dashboard_layout);
		newsButton = (Button) findViewById(R.id.btn_news);
		settingsButton = (Button) findViewById(R.id.btn_settings);
		infoButton = (Button) findViewById(R.id.btn_info);
		loginButton = (Button) findViewById(R.id.btn_login);
		
			setTypeFaces();
		
			new AsyncActionClass().execute(null);
		
			addListenerOSettingsButton();
			
			addListenerOnInfoButton();
			
			addListenerOnNewsButton();
			
			addListenerOnLoginButton();
			
			
	}


	private void setTypeFaces() {
		newsButton.setTypeface(basicFontTypeFace);
		settingsButton.setTypeface(basicFontTypeFace);
		infoButton.setTypeface(basicFontTypeFace);
		loginButton.setTypeface(basicFontTypeFace);
	}


	private void addListenerOnLoginButton() {
		loginButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
				startActivity(intent);
			}
		});
		
	}
	
	private void addListenerOnLogoutButton() {
		loginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
						   JPdroidApplication app = (JPdroidApplication)getApplicationContext();
							app.setLoggedUser(null);
							sharedPreferenceEditor.remove("username");
							sharedPreferenceEditor.remove("password");
							sharedPreferenceEditor.commit();
							Toast.makeText(getApplicationContext(), "Wylogowano pomyślnie.", Toast.LENGTH_SHORT).show();
							doAfterLogout();
					   }
					
		});
			
		
	}


	private void initializeUser() {
		JPdroidApplication app = (JPdroidApplication)getApplicationContext();
		 String username = sharedPreferences.getString("username", null);
		 String password = sharedPreferences.getString("password", null);
		 if(username!=null && password!=null && !app.isUserLogged()){
				 UserManager userManager = new UserManager();
				 try {
					user = userManager.getUser(username, password);
					app.setLoggedUser(user);
				} catch (Exception e) {
					app.setLoggedUser(null);
					Toast.makeText(getApplicationContext(), "Błąd! Nie zalogowano usera!", Toast.LENGTH_LONG).show();
				}
			 
		 }
		 
		ImageView loggedUserAvatar = (ImageView)findViewById(R.id.footer_user_avatar);
		TextView footerTextView = (TextView)findViewById(R.id.footer_text);
		ImageLoader imageLoader; 
			
			if(app.isUserLogged()){
				user = ((JPdroidApplication)getApplicationContext()).getLoggedUser();
				loggedUserAvatar.setVisibility(View.VISIBLE);
				imageLoader=new ImageLoader(getApplicationContext());
				if(user.getAvatarSource()!=null && user.getAvatarSource().length()>0){
					
					imageLoader.DisplayImage("http://forum.juvepoland.com/images/avatars/"+user.getAvatarSource(), loggedUserAvatar); //TODO : Zastąpić pobieraniem z pliku .properties
				}
				
				footerTextView.setText("Witaj, "+user.getUsername()+"!");
				loginButton.setText("Wyloguj");
				addListenerOnLogoutButton();
				
			}
			else{
				loggedUserAvatar.setVisibility(LinearLayout.GONE);
				footerTextView.setText("www.JuvePoland.com");	//TODO wziac z pliku
				loginButton.setText("Zaloguj");
				addListenerOnLoginButton();
				
			}
			footerTextView.setTypeface(basicFontTypeFace);
		
	}


	private void addListenerOnInfoButton() {
			infoButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent infoActivityIntent = new Intent(getApplicationContext(), InfoActivity.class);
				startActivity(infoActivityIntent);
				
			}
		});
	}


	private void addListenerOnNewsButton() {
		newsButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent newsActivityIntent = new Intent(getApplicationContext(), NewsActivity.class);
				startActivity(newsActivityIntent);
				
			}
		});
		
	}


	private void addListenerOSettingsButton() {
		
		settingsButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent settingIntent = new Intent(getApplicationContext(), MyPreferencesActivity.class);
				startActivity(settingIntent);
			}
		});
		
	}
	
	



	@Override
	protected void onResume() {
		super.onResume();
		initializeUser();
		setTypeFaces();
	}


	@Override
	protected void doInBackgroundAsyncProcess() throws Exception {
		initializeUser();	
	}


	@Override
	protected void doAfterLogout() {
		onResume();
	}
	
}
